package myJs.user

import myJs.Implicits._
import myJs.Utils._
import scala.scalajs.js.Dynamic.{global=>g}
import myJs.myPkg.{DistpickerOptions, LayerOptions}
import myJs.myPkg.jquery._
import myJs.tool.Tool
import myJs.tool.Tool._
import org.scalajs.dom.{FormData, document}
import org.scalajs.dom.ext.Ajax
import scalatags.Text.all.s

import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}

/**
 * Created by yz on 2019/4/25
 */
@JSExportTopLevel("UpdatePersonInfo")
object UpdatePersonInfo {

  @JSExport("init")
  def init = {
    refreshPersonInfo
    bootStrapValidator

  }

  def refreshPersonInfo = {
    val url = g.jsRoutes.controllers.UserController.getUserById().url.toString
    Ajax.get(url = s"${url.noCache}&id=${getId}", headers = Map("Content-Type" -> "application/json")).map { xhr =>
      val rs = xhr.responseText.toJDJAn.toMyJDS
      Tool.fillByNames(rs)
      val options = DistpickerOptions.province(rs("province")).city(rs("city"))
      $("#distpicker").distpicker(options)
    }
  }

  def getId = {
    g.id.toString
  }

  def bootStrapValidator = {
    val emailCheckUrl = g.jsRoutes.controllers.AppController.emailCheck().url.toString
    val maxNumber = Double.MaxValue
    val dict: js.Dictionary[js.Any] = js.Dictionary(
      "feedbackIcons" -> js.Dictionary(
        "valid" -> "glyphicon glyphicon-ok",
        "invalid" -> "glyphicon glyphicon-remove",
        "validating" -> "glyphicon glyphicon-refresh",
      ),
      "fields" -> js.Dictionary(
        "fullName" ->{
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" ->s"${messages("null")}"
              ),
            )
          )
        } ,
        "institution" ->{
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> s"${messages("null")}"
              ),
            )
          )
        } ,
        "teacher" ->{
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> s"${messages("null")}"
              ),
            )
          )
        } ,
      )
    )
    $(s"#form").bootstrapValidator(dict)
  }


}
